home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Games / Solitaire / Sources / Spider / Spider.m < prev    next >
Text File  |  1994-04-21  |  8KB  |  291 lines

  1. /* indent:4  tabsize:8  font:fixed-width */
  2.  
  3. #import "Spider.h"
  4. #import "localstrings.h"
  5. #import "SpiderDrawDelegate.h"
  6. #import "SpiderSuitDelegate.h"
  7. #import "SpiderGameDelegate.h"
  8.  
  9. @implementation Spider
  10.  
  11.  
  12. /*---------------------------------------------------------------------------
  13. |
  14. |    - startGame:
  15. |
  16. |    returns:    (id)  self
  17. |
  18. |----------------------------------------------------------------------------
  19. |
  20. |    Start a new game.  Get confirmation from the user before aborting a 
  21. |    game in progress.
  22. |            
  23. \----------------------------------------------------------------------------*/
  24.  
  25. - startGame:sender
  26. {
  27.     [super startGame:sender];
  28.     return [self setupGame:YES];
  29. }
  30.  
  31.  
  32. /*---------------------------------------------------------------------------
  33. |
  34. |    - restartGame:
  35. |
  36. |    returns:  (id) self
  37. |
  38. |----------------------------------------------------------------------------
  39. |
  40. |    Restart the game in progress.
  41. |            
  42. \----------------------------------------------------------------------------*/
  43.  
  44. - restartGame:sender
  45. {
  46.     [super restartGame:sender];
  47.     return [self setupGame:NO];
  48. }
  49.  
  50.  
  51. /*---------------------------------------------------------------------------
  52. |
  53. |    - setupGame:(BOOL)redeal
  54. |
  55. |    returns:  (id) self
  56. |
  57. |----------------------------------------------------------------------------
  58. |
  59. |    Setup a new game.  If "redeal" is true, deal a new deck, otherwise
  60. |    use the same cards as the previous game.
  61. |            
  62. \----------------------------------------------------------------------------*/
  63.  
  64. - setupGame:(BOOL)redeal
  65. {
  66.     int pileIndex, i;
  67.     int cycle;
  68.     id  suitCardPiles[8];
  69.     id  gameCardPiles[10];
  70.     id  drawCardPile;
  71.  
  72.     [gameWindow disableFlushWindow];
  73.  
  74.     //----------------------------------------------------------------
  75.     //    Sync pile id's with current game window and set 
  76.     //    preferences and delegates
  77.     //----------------------------------------------------------------
  78.     
  79.     suitCardPiles[0] = suitPileView1;
  80.     suitCardPiles[1] = suitPileView2;
  81.     suitCardPiles[2] = suitPileView3;
  82.     suitCardPiles[3] = suitPileView4;
  83.     suitCardPiles[4] = suitPileView5;
  84.     suitCardPiles[5] = suitPileView6;
  85.     suitCardPiles[6] = suitPileView7;
  86.     suitCardPiles[7] = suitPileView8;
  87.  
  88.     for (i = 0; i < 8; i++)
  89.     {
  90.         [suitCardPiles[i] setBackgroundColor:desktopColor];
  91.         [suitCardPiles[i] setCardSize:cardSize];
  92.         [suitCardPiles[i] setDelegate:suitPileDelegate];
  93.     }
  94.     
  95.     gameCardPiles[0] = gamePileView1;
  96.     gameCardPiles[1] = gamePileView2;
  97.     gameCardPiles[2] = gamePileView3;
  98.     gameCardPiles[3] = gamePileView4;
  99.     gameCardPiles[4] = gamePileView5;
  100.     gameCardPiles[5] = gamePileView6;
  101.     gameCardPiles[6] = gamePileView7;
  102.     gameCardPiles[7] = gamePileView8;
  103.     gameCardPiles[8] = gamePileView9;
  104.     gameCardPiles[9] = gamePileView10;
  105.     for (i = 0; i < 10; i++)
  106.     {
  107.         [gameCardPiles[i] setBackgroundColor:desktopColor];
  108.         [gameCardPiles[i] setCardSize:cardSize];
  109.         [gameCardPiles[i] setDelegate:gamePileDelegate];
  110.     }
  111.     
  112.     [drawPileView setBackgroundColor:desktopColor];
  113.     [drawPileView setCardSize:cardSize];
  114.     drawCardPile = [drawPileView cardPile];
  115.     [drawPileDelegate setGameCardPileViews:gameCardPiles];
  116.     [drawPileView setDelegate:drawPileDelegate];
  117.     
  118.     [gameWindow display];
  119.     [gameWindow reenableFlushWindow];
  120.     [gameWindow flushWindow];
  121.  
  122.     //-----------------------------------------------------------------------
  123.     //    Initialize the drawPileView to have a 2 shuffled decks.
  124.     //-----------------------------------------------------------------------
  125.     
  126.     [drawCardPile freeCards];
  127.     if (redeal)
  128.     {
  129.         [drawCardPile addDeck];
  130.         [[drawCardPile addDeck] shuffle];
  131.     
  132.     // make a copy of the CardPile for restart option
  133.     if (!prevDeck)
  134.     {
  135.         prevDeck = [[CardPile allocFromZone:[self zone]]
  136.                                initForCardSize:cardSize];
  137.     }
  138.     else
  139.     {
  140.         [prevDeck freeCards];
  141.         [prevDeck setCardSize:cardSize];
  142.     }
  143.     [prevDeck addCopyOfPile:drawCardPile];
  144.     }
  145.     else
  146.     {
  147.         if (prevDeck)
  148.     {
  149.         // copy the saved deck back to the game deck
  150.         [prevDeck setCardSize:cardSize];
  151.         [drawCardPile addCopyOfPile:prevDeck];
  152.     }
  153.     else
  154.     {
  155.         // this shouldn't happen, but just in case...
  156.             [drawCardPile freeCards];
  157.         [drawCardPile addDeck];
  158.         [[drawCardPile addDeck] shuffle];
  159.     }
  160.     }
  161.     
  162.     //-----------------------------------------------------------------------
  163.     //    Initialize the "suit piles" as empty
  164.     //-----------------------------------------------------------------------
  165.  
  166.     for (pileIndex = 0; pileIndex < 8; pileIndex++)
  167.     {
  168.         [[suitCardPiles[pileIndex] cardPile] freeCards];
  169.         [suitCardPiles[pileIndex] display];
  170.     }
  171.  
  172.     //-----------------------------------------------------------------------
  173.     //    Initialize and deal cards to the 10 "user piles"
  174.     //-----------------------------------------------------------------------
  175.  
  176.     for (pileIndex = 0; pileIndex < 10; pileIndex++)
  177.     {
  178.         [[gameCardPiles[pileIndex] cardPile] freeCards];
  179.     }
  180.     
  181.     for (cycle = 0; cycle < 4; cycle++)
  182.     {
  183.     for (pileIndex = 0; pileIndex < 10; pileIndex++)
  184.     {
  185.         id userPile = [gameCardPiles[pileIndex] cardPile];
  186.         id tempCard = [drawCardPile cardAt:CS_TOP];
  187.         [drawCardPile removeCard:tempCard];
  188.         [userPile insertCard:tempCard at:CS_TOP];
  189.         [gameCardPiles[pileIndex] display];
  190.     }
  191.     }
  192.     
  193.     for (pileIndex = 0; pileIndex < 4; pileIndex++)
  194.     {
  195.         id userPile = [gameCardPiles[pileIndex] cardPile];
  196.     id tempCard = [drawCardPile cardAt:CS_TOP];
  197.     [drawCardPile removeCard:tempCard];
  198.     [userPile insertCard:tempCard at:CS_TOP];
  199.     [gameCardPiles[pileIndex] display];
  200.     }
  201.  
  202.     for (pileIndex = 0; pileIndex < 10; pileIndex++)
  203.     {
  204.     id userPile = [gameCardPiles[pileIndex] cardPile];
  205.     id tempCard = [drawCardPile cardAt:CS_TOP];
  206.     [drawCardPile removeCard:tempCard];
  207.     [userPile insertCard:tempCard at:CS_TOP];
  208.     [[userPile cardAt:CS_TOP] flip];
  209.     [gameCardPiles[pileIndex] display];
  210.     }
  211.  
  212.     [gameWindow display];
  213.     [gameWindow makeKeyAndOrderFront:self];
  214.     return self;
  215. }
  216.  
  217.  
  218. /*---------------------------------------------------------------------------
  219. |
  220. |    - endGame:sender
  221. |
  222. |    returns:  (id) self
  223. |
  224. |----------------------------------------------------------------------------
  225. |
  226. |    End the game in progress.  Discard the game window.
  227. |            
  228. \----------------------------------------------------------------------------*/
  229.  
  230. - endGame:sender
  231. {
  232.     // close the game window
  233.     [super endGame:sender];
  234.  
  235.     // ****custom code here****
  236.     
  237.     return self;
  238. }
  239.  
  240. /*---------------------------------------------------------------------------
  241. |
  242. |    - win
  243. |
  244. |     returns: (id) self
  245. |
  246. |----------------------------------------------------------------------------
  247. |
  248. |    Called when the game has been won.  This is where you can insert fancy
  249. |    winning routines, or just call the default (boring) routine.
  250. |
  251. \----------------------------------------------------------------------------*/
  252.  
  253. - win
  254. {
  255.     [super win];  // replace this with something wonderful
  256.     return self;
  257. }
  258.  
  259.  
  260. /*---------------------------------------------------------------------------
  261. |
  262. |    - checkForWin
  263. |
  264. |    returns: (id) self
  265. |
  266. |----------------------------------------------------------------------------
  267. |
  268. |    Called to check the state of the game.  Always override (unless your
  269. |    game is impossible to win).
  270. |
  271. \----------------------------------------------------------------------------*/
  272.  
  273. - checkForWin
  274. {
  275.     if ([[suitPileView1 cardPile] cardCount] &&
  276.         [[suitPileView2 cardPile] cardCount] &&
  277.     [[suitPileView3 cardPile] cardCount] &&
  278.     [[suitPileView4 cardPile] cardCount] &&
  279.     [[suitPileView5 cardPile] cardCount] &&
  280.     [[suitPileView6 cardPile] cardCount] &&
  281.     [[suitPileView7 cardPile] cardCount] &&
  282.     [[suitPileView8 cardPile] cardCount])
  283.     {
  284.         [self win];
  285.     }
  286.     return self;
  287. }
  288.  
  289. @end
  290.  
  291.